package com.china08.yxyapi.config;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.pac4j.core.config.Config;
import org.pac4j.springframework.web.SecurityInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.WebContentInterceptor;

import com.china08.yxyapi.security.JwtProfileUtil;

/**
 * SecurityConfig
 * 
 * @author Ertao.Fang
 *
 */
@Configuration
@ComponentScan(basePackages = "org.pac4j.springframework.web")
public class SecurityConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private Config config;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistry ir = registry;
        // ##
        // 全局拦截
        registry.addInterceptor(new WebContentInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                    throws ServletException {
                JwtProfileUtil.getProfile(request, response);
                return super.preHandle(request, response, handler);
            }
        }).addPathPatterns("*");

        // 活动
        // ir.addInterceptor(new YxySi(config, "HeaderClient",
        // "teacher")).addPathPatterns("/api*/home/activity/tea/**");

        ir.addInterceptor(new YxySi(config, "HeaderClient,ParameterClient"))
                .addPathPatterns("/api*/comm/uploadImgForCk");

        ir.addInterceptor(new YxySi(config, "HeaderClient,ParameterClient"))
                .addPathPatterns("/api*/manage/tec/homework/question/ueserver");
        // 微课按目录保存
        ir.addInterceptor(new YxySi(config, "HeaderClient,ParameterClient"))
                .addPathPatterns("/api*/home/weike/content/record");
        // 微课按目录初始化
        ir.addInterceptor(new YxySi(config, "HeaderClient,ParameterClient"))
                .addPathPatterns("/api*/home/weike/content/init");

        // 保存学段
        ir.addInterceptor(new YxySi(config, "HeaderClient,ParameterClient")).addPathPatterns("/api*/home/weike/record");
        // 初始化方法 资源方法
        ir.addInterceptor(new YxySi(config, "HeaderClient,ParameterClient")).addPathPatterns("/api*/home/res/record");

        // 初始化方法 微课
        ir.addInterceptor(new YxySi(config, "HeaderClient,ParameterClient")).addPathPatterns("/api*/home/weike/init");

        // ##
        // 公共上传
        ir.addInterceptor(new YxySi(config, "HeaderClient,ParameterClient")).addPathPatterns("/api*/comm/upload**");
        // 微课详情 FIXME(活动结束去除)
        // ir.addInterceptor(new YxySi(config,
        // "HeaderClient,ParameterClient")).addPathPatterns("/api*/home/weike/detail");
        // 收藏
        ir.addInterceptor(new YxySi(config, "HeaderClient,ParameterClient"))
                .addPathPatterns("/api*/home/weike/fav/crt");
        // 评价
        ir.addInterceptor(new YxySi(config, "HeaderClient,ParameterClient"))
                .addPathPatterns("/api*/home/weike/mark/crt");
        // 资源详情
        // ir.addInterceptor(new YxySi(config,
        // "HeaderClient,ParameterClient")).addPathPatterns("/api*/home/res/detail");
        // 导出模板：考试成绩
        ir.addInterceptor(new YxySi(config, "HeaderClient,ParameterClient"))
                .addPathPatterns("/api_yw/manage/tec/teaching/scoreport/selectexam/exportexam/**");

        ir.addInterceptor(new YxySi(config, "HeaderClient,ParameterClient"))
                .addPathPatterns("/api_yw/home/kecheng/xiaoben/**");

        // ##在线组卷
        // 导出答题卡
        ir.addInterceptor(new YxySi(config, "HeaderClient,ParameterClient"))
                .addPathPatterns("/api_yw/manage/tec/teaching/zujuan/answer/export/**");
        // 下载word试卷
        ir.addInterceptor(new YxySi(config, "HeaderClient,ParameterClient"))
                .addPathPatterns("/api_yw/manage/tec/teaching/zujuan/paper/export/**");

        // 慧学--查询知识点下是否存在试题
        ir.addInterceptor(new YxySi(config, "HeaderClient,ParameterClient"))
                .addPathPatterns("/api_yw/manage/stu/mystudy/huixue/exercise/getid/**");
        // 慧学--查询知识点下是否存在微课
        ir.addInterceptor(new YxySi(config, "HeaderClient,ParameterClient"))
                .addPathPatterns("/api_yw/manage/stu/mystudy/huixue/scoreimprove/isnot/weike/**");

        // 学生报告——查看学生报告
        ir.addInterceptor(new YxySi(config, "HeaderClient,ParameterClient"))
                .addPathPatterns("/api_yw/h5/par/study/scoreport/stureport/getone/**");
        // 学生报告——感谢教师
        ir.addInterceptor(new YxySi(config, "HeaderClient,ParameterClient"))
                .addPathPatterns("/api_yw/h5/par/study/scoreport/stureport/thank/**");

        // ##
        // 公共拦截
        ir.addInterceptor(new YxySi(config, "HeaderClient"))//
                .addPathPatterns("/api*/**")//
                .excludePathPatterns("/api*/auth/sessions/**")//
                .excludePathPatterns("/api*/home/index/**")//
                .excludePathPatterns("/api*/home/res/**")//
                .excludePathPatterns("/api*/home/weike/**")//
                .excludePathPatterns("/api*/home/room/**")//
                .excludePathPatterns("/api*/comm/**")//
                .excludePathPatterns("/api*/schwebsite/**")//
                .excludePathPatterns("/api*/temp/**")//
                .excludePathPatterns("/api*/home/activity/**")//
                .excludePathPatterns("/api*/home/wk/**")//
                .excludePathPatterns("/api*/home/webgroup/**")//
                .excludePathPatterns("/api*/home/kecheng/**")//
                .excludePathPatterns("/api*/home/marker/**");

        // ##
        // 空间:教师
        ir.addInterceptor(new YxySi(config, "HeaderClient", "teacher")).addPathPatterns("/api*/manage/tec/**");
        // 空间:学生
        ir.addInterceptor(new YxySi(config, "HeaderClient", "student")).addPathPatterns("/api*/manage/stu/**");
        // 家长可以访问学生的功能
        ir.addInterceptor(new YxySi(config, "HeaderClient", "parents")).addPathPatterns("/api*/manage/stu/moments/**");

        ir.addInterceptor(new YxySi(config, "HeaderClient", "parents")).addPathPatterns("/api*/manage/stu/mystudy/**");
        // 空间:家长
        ir.addInterceptor(new YxySi(config, "HeaderClient", "parents")).addPathPatterns("/api*/manage/par/**");

        // 空间:学校
        ir.addInterceptor(new YxySi(config, "HeaderClient", "schadmin")).addPathPatterns("/api*/manage/sch/**");
        // 空间:教育局
        ir.addInterceptor(new YxySi(config, "HeaderClient", "eduadmin")).addPathPatterns("/api*/manage/edu/**");
        // ##
        // 个人信息
        ir.addInterceptor(new YxySi(config, "HeaderClient", "profile")).addPathPatterns("/api*/personal/profile**");
    }

    /**
     * Yxy Custom Security Interceptor
     * 
     * @author Ertao.Fang
     *
     */
    static class YxySi extends YxySecurityInterceptor {
        public YxySi(Config config, String clients, String authorizers, String matchers) {
            super(config, clients, authorizers, matchers);
        }

        public YxySi(Config config, String clients, String authorizers) {
            super(config, clients, authorizers);
        }

        public YxySi(Config config, String clients) {
            super(config, clients);
        }

        public YxySi(Config config) {
            super(config);
        }

    }

    /**
     * Yxy Custom Security Interceptor
     * 
     * @author Ertao.Fang
     *
     */
    static class YxySecurityInterceptor extends SecurityInterceptor {

        public YxySecurityInterceptor(Config config) {
            super(config);
        }

        public YxySecurityInterceptor(Config config, String clients) {
            super(config, clients);
        }

        public YxySecurityInterceptor(Config config, String clients, String authorizers) {
            super(config, clients, authorizers);
        }

        public YxySecurityInterceptor(Config config, String clients, String authorizers, String matchers) {
            super(config, clients, authorizers, matchers);
        }

        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            // ##
            // 跨域请求:第一次OPTIONS请求:返回200
            if ("OPTIONS".equals(request.getMethod())) {
                response.setStatus(HttpStatus.OK.value());
                return true;
            }
            // ##
            // 跨域请求
            else {
                // ##
                // 添加跨域请求Header:解决vue-resource跨域请求,403状态码无法获取问题
                // response.addHeader("Access-Control-Allow-Origin", "*");
                // response.addHeader("Access-Control-Allow-Credentials",
                // "true");
                // ##
                // 权限认证
                return super.preHandle(request, response, handler);
            }
        }

    }
}
